package org.jeecg.modules.nikeProject;


import org.apache.commons.lang.math.RandomUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ProjectCodeUtils {

    private static final String prefix = "NIKE";

    /**
     * 随即编码
     */
    private static final int[] r = new int[]{7, 9, 6, 2, 8, 1, 3, 0, 5, 4};

    private static StringBuilder toCode(String shopOrderId) {
        String idStr = shopOrderId.toString();
        StringBuilder idsbs = new StringBuilder();
        for (int i = idStr.length() - 1; i >= 0; i--) {
            idsbs.append(r[idStr.charAt(i) - '0']);
        }
        return idsbs.append(getRandom());
    }

    /**
     * 生成时间戳
     */
    private static String getDateTime() {
        DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(new Date());
    }
    /**
     * 生成不带类别标头的编码
     */
    private static synchronized String getCode(String shopOrderId) {
        shopOrderId = shopOrderId == null ? "" : shopOrderId;
        return getDateTime() + toCode(shopOrderId);
    }

    /**
     * 生成固定长度随机码
     */
    private static int getRandom() {
        int random = RandomUtils.nextInt(90) + 10;
        return random;
    }

    /**
     * 生成订单单号编码(调用方法)
     */
    public static String getProjectCode(String shopOrderId) {
        return prefix + getCode(shopOrderId);
    }

    public static void main(String[] args) {
        System.out.println("固定长度的随机编码：" + getRandom());
        String code = "1610559865520619522";
        code = code.substring(code.length() - 4, code.length());
        System.out.println("code：" + code);
        System.out.println("随机生成项目编号：" + getProjectCode(code));
    }
}
